package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPublishServiceNodeBinding;
import com.sgb.kjwl.model.entity.enterprise.ServiceNodeListEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.MemberEvent;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceNodeAdpater;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceNodeViewModel;
import com.sgb.kjwl.viewmodel.enterprise.ServiceNodeListModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

public class PublishServiceNodeActivity extends AppActivity {
    private PublishServiceNodeViewModel mViewModel2;
    private ServiceNodeListModel mViewModel;
    private ActivityPublishServiceNodeBinding mBinding;
    private ServiceNodeAdpater madpater;
    private String id;



    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(ServiceNodeListModel.class);
        mViewModel2 = new ViewModelProvider(this).get(PublishServiceNodeViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_publish_service_node);
        mBinding.setVariable(BR.mViewModel, mViewModel);
        mBinding.setLifecycleOwner(this);
        id = getIntent().getStringExtra("data");
        initRecyclerView();
        initObserver();
        initLister();
    }

    private void initRecyclerView(){
        madpater = new ServiceNodeAdpater(this,true);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.shape_list_divider_10dp));
        mBinding.recyclerView.addItemDecoration(divider);
        mBinding.recyclerView.setAdapter(madpater);
        madpater.setOnItemClick(new AdapterOnItemClick<ServiceNodeListEntity>() {
            @Override
            public void onItemClick(ServiceNodeListEntity serviceNodeListEntity, int position) {
                switch (position){
                    case -1://修改
                        AddServiceNodeActivity.startactivity(PublishServiceNodeActivity.this,"",serviceNodeListEntity);
                        break;
                    case -2://删除
                        com.sgb.kjwl.utils.DialogHelper.showDialog(PublishServiceNodeActivity.this, "温馨提示", "是否确认删除？", "确定", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                mViewModel2.deleteServiceNode(serviceNodeListEntity.getId());
                            }
                        });

                        break;
                }

            }
        });

        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getServiceNodeList(false,id);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getServiceNodeList(true,id);
            }
        });
    }


    private void initLister(){
        mBinding.actionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                AddServiceNodeActivity.startactivity(PublishServiceNodeActivity.this,id,null);
            }
        });

        mBinding.tvOk.setOnClickListener(view ->{
            if (!ClickUtils.isDoubleClick()) {


                EventBus.getDefault().postSticky(MemberEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_SERVICE_FINISH));
                EventBus.getDefault().postSticky(MemberEvent.make().setMessageTag(EventConstant.EVENT_PUBLISH_SERVICE_FINISHDealedCases));
                MToast.showToast(PublishServiceNodeActivity.this, "资料保存成功");
                // TODO: 2021/4/16 跳转服务列表页
                ActivityJump.jumpToActivity(PublishServiceNodeActivity.this, PublishServiceListActivity.class);
                finish();
            }
        });
        mBinding.tvBack.setOnClickListener(view ->{
            finish();
        });
    }



    private void initObserver(){
        mViewModel.serviceNodeList.observe(this,listdata->{
            if(listdata!=null) {
                if (mViewModel.pageSize == 1) {
                    mBinding.recyclerView.refreshComplete();
                    madpater.setDatas(listdata);
                    if(listdata.size()==0){
                        mBinding.empty.setVisibility(View.VISIBLE);
                        mBinding.recyclerView.setVisibility(View.GONE);
                    }else{
                        mBinding.empty.setVisibility(View.GONE);
                        mBinding.recyclerView.setVisibility(View.VISIBLE);
                    }
                } else {
                    mBinding.recyclerView.loadMoreComplete();
                    madpater.addDatas(listdata);
                    if (listdata.size() < 10) {
                        mBinding.recyclerView.setNoMore(true);
                    }
                }

            }

        });
        mViewModel.ShowLoading.observe(this,type->{
            switch (type){
                case 1:
                    DialogHelper.showProgressDialog(PublishServiceNodeActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
        mViewModel.toast.observe(this,message->MToast.showLongToast(this,message));
        mViewModel2.ShowLoading.observe(this,type->{
            switch (type){
                case 1:
                    DialogHelper.showProgressDialog(PublishServiceNodeActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;

                case 3:
                    mViewModel.getServiceNodeList(false,id);
                    break;
            }
        });
        mViewModel2.toast.observe(this,message->MToast.showLongToast(this,message));
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!TextUtils.isEmpty(id)) {
            mViewModel.getServiceNodeList(false, id);
        }
    }

    public static void startactivity(Context mContext, String id){
        Intent mIntent = new Intent(mContext,PublishServiceNodeActivity.class);
        mIntent.putExtra("data",id);
        mContext.startActivity(mIntent);

    }


}
